package xin.yangshuai.activemqgeneral.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


/**
 * JmsProducerTopic
 *
 * @author shuai
 * @date 2020/6/21
 */
public class JmsProducerTopic {

    public static final String ACTIVEMQ_URL = "tcp://192.168.124.7:61616";
    public static final String TOPIC_NAME = "topic01";

    public static void main(String[] args) throws JMSException, InterruptedException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageProducer messageProducer = session.createProducer(session.createTopic(TOPIC_NAME));

        for (int i = 0; i < 30; i++) {
            TextMessage textMessage = session.createTextMessage("topic-msg---" + (i + 1));
            Thread.sleep(1000);
            messageProducer.send(textMessage);
        }

        messageProducer.close();
        session.close();
        connection.close();

        System.out.println("*****TOPIC消息发布到MQ完成");

    }
}
